package modelo.dao.hibernate.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class PersistenciaJPA {
	private static EntityManagerFactory factory;
	private static EntityManager entityManager;
	
	static {
		if(factory == null) {
			factory = Persistence.createEntityManagerFactory("AGENDA_TELEFONICA");	
		}
	}
	
	public PersistenciaJPA(String unidadePersistencia) {	
		factory = Persistence.createEntityManagerFactory(unidadePersistencia);
	}
	
	public static EntityManager getEntityManager(){
		
		entityManager = factory.createEntityManager();
		
		return entityManager;
	}
	
	public static void closeEntityManager(){
		if(entityManager.isOpen()) {
			entityManager.close();			
		}		
	}
	
}
